<?php
App::uses('AppController', 'Controller');
/**
 * Rates Controller
 *
 * @property Rate $Rate
 * @property PaginatorComponent $Paginator
 */
class RatesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Rate->recursive = 0;
		$this->set('rates', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 
	public function admin_view($id = null) {
		if (!$this->Rate->exists($id)) {
			throw new NotFoundException(__('Invalid rate'));
		}
		$options = array('conditions' => array('Rate.' . $this->Rate->primaryKey => $id));
		$this->set('rate', $this->Rate->find('first', $options));
	}
*/
/**
 * add method
 *
 * @return void
 
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Rate->create();
			if ($this->Rate->save($this->request->data)) {
				$this->Session->setFlash(__('The rate has been saved.'));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The rate could not be saved. Please, try again.'));
			}
		}
		$games = $this->Rate->Game->find('list');
		$users = $this->Rate->User->find('list');
		$this->set(compact('games', 'users'));
	}
*/
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 
	public function admin_edit($id = null) {
		if (!$this->Rate->exists($id)) {
			throw new NotFoundException(__('Invalid rate'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Rate->save($this->request->data)) {
				$this->Session->setFlash(__('The rate has been saved.'));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The rate could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Rate.' . $this->Rate->primaryKey => $id));
			$this->request->data = $this->Rate->find('first', $options);
		}
		$games = $this->Rate->Game->find('list');
		$users = $this->Rate->User->find('list');
		$this->set(compact('games', 'users'));
	}
*/
/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Rate->id = $id;
		if (!$this->Rate->exists()) {
			throw new NotFoundException(__('Invalid rate'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Rate->delete()) {
			$this->Session->setFlash(__('The rate has been deleted.'));
		} else {
			$this->Session->setFlash(__('The rate could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
	public function add() {
		if ($this->request->is('post')) {
			// pr($this->request->params); exit;
			$user_info = $this->get_user();
			if($user_info['id'] == '' || $user_info['id'] == NULL){
				$this->Session->setFlash('Bạn phải đăng nhập mới có thể thực hiện chức năng này');
				return $this->redirect($this->referer());
			}else{
				$this->request->data['Rate']['user_id'] = $user_info['id'];
				$this->request->data['Rate']['game_id'] = $this->request->params['pass'][1];
				$this->request->data['Rate']['point'] = $this->request->params['pass'][0];
				$data = $this->Rate->find('count',
					array(
						'conditions' => array(
							'Rate.game_id' => $this->request->data['Rate']['game_id'],
							'Rate.user_id' => $user_info['id']
						)
					)
				);
				
				if($data == 0){
					$this->Rate->create();
					if ($this->Rate->save($this->request->data)) {
						$this->Session->setFlash(__('Cảm ơn bạn đã bình chọn.'));
						return $this->redirect($this->referer());
					} else {
						$this->Session->setFlash(__('Có lỗi trong việc bình chọn. Vui lòng thử lại'));
					}
				}else {
					$this->Session->setFlash(__('Bạn đã bình chọn rồi. Xin cảm ơn'));
					return $this->redirect($this->referer());
				}
			}
		}
	}
}
